//
//  LXStatusFrame.m
//  微了个小博
//
//  Created by imera on 15/9/27.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXStatusFrame.h"
#import "LXStatus.h"
#import "LXUser.h"

#define LXStatusBorder 10

@implementation LXStatusFrame

- (void)setStatus:(LXStatus *)status
{
    _status = status;

    //得到模型数据，给模型设置frame LXStatus
    
    //顶部TopView
    CGFloat topViewFX = 0;
    CGFloat topViewFY = 0;
    CGFloat topViewFW = [[UIScreen mainScreen] bounds].size.width - LXStatusTableBorder * 2;
    CGFloat topViewFH = 0;
    
    //用户的头像
    CGFloat profile_image_urlFX = LXStatusBorder;
    CGFloat profile_image_urlFY = LXStatusBorder;
    CGFloat profile_image_urlFW = 50;
    CGFloat profile_image_urlFH = 50;
    self.profile_image_urlF = CGRectMake(profile_image_urlFX, profile_image_urlFY, profile_image_urlFW, profile_image_urlFH);
    
    //用户的昵称
    CGFloat nameFX = CGRectGetMaxX(self.profile_image_urlF) + LXStatusBorder;
    CGFloat nameFY = LXStatusBorder;
    CGSize nameLabelSize = [status.user.screen_name sizeWithAttributes:@{NSFontAttributeName:LXStatusNameFont}];
    CGFloat nameFW = nameLabelSize.width;
    CGFloat nameFH = nameLabelSize.height;
    self.nameF = CGRectMake(nameFX, nameFY, nameFW, nameFH);
    
    //用户的会员等级
    if(status.user.mbtype){
        CGFloat vipViewFX = CGRectGetMaxX(self.nameF) + LXStatusBorder;
        CGFloat vipViewFY = LXStatusBorder;
        CGFloat vipViewFW = 14;
        CGFloat vipViewFH = nameFH;
        self.vipViewF = CGRectMake(vipViewFX, vipViewFY, vipViewFW, vipViewFH);
    }
    
    //微博的创建时间
    CGFloat created_atFX = nameFX;
    CGFloat created_atFY = CGRectGetMaxY(self.nameF) + LXStatusBorder;
    CGSize createdSize = [status.created_at sizeWithAttributes:@{NSFontAttributeName:LXStatusTimeFont}];
    CGFloat created_atFW = createdSize.width;
    CGFloat created_atFH = createdSize.height;
    self.created_atF = CGRectMake(created_atFX, created_atFY, created_atFW, created_atFH);
    
    //微博的来源
    CGFloat sourceFX = CGRectGetMaxX(self.created_atF) + LXStatusBorder;
    CGFloat sourceFY = created_atFY;
    CGSize sourceFSize = [status.source sizeWithAttributes:@{NSFontAttributeName:LXStatusTimeFont}];
    CGFloat sourceFW = sourceFSize.width;
    CGFloat sourceFH = sourceFSize.height;
    self.sourceF = CGRectMake(sourceFX, sourceFY, sourceFW, sourceFH);
    
    //微博的正文
    CGFloat textFX = LXStatusBorder;
    CGFloat textFY = CGRectGetMaxY(self.profile_image_urlF) + LXStatusBorder;
    CGFloat contentViewMaxW = topViewFW - 2*LXStatusBorder;
    CGSize textFSize = [status.text boundingRectWithSize:CGSizeMake(topViewFW - LXStatusBorder, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:LXStatusTextFont} context:nil].size;
    CGFloat textFW = textFSize.width;
    CGFloat textFH = textFSize.height;
    self.textF = CGRectMake(textFX, textFY, textFW, textFH);
    
    //微博的配图
    if(status.pic_urls.count){
        CGFloat thumbnail_picFX = textFX;
        CGFloat thumbnail_picFY = CGRectGetMaxY(self.textF) + LXStatusBorder;
        CGFloat thumbnail_picFW = contentViewMaxW - 2*LXStatusBorder;
        
        int maxCloumns = (status.pic_urls.count == 4) ? 2 : 3;
        int row = (status.pic_urls.count / (double)maxCloumns) + 0.99;
        CGFloat thumbnail_picFH = LXStatusPhotoRowHeight * row;
        
        self.pic_urlsF = CGRectMake(thumbnail_picFX, thumbnail_picFY, thumbnail_picFW, thumbnail_picFH);
    }
    
    //被转发的微博
    if(status.retweeted_status){
        //被转发微博的父控件
        CGFloat retweetedViewFX = LXStatusBorder;
        CGFloat retweetedViewFY = CGRectGetMaxY(self.textF);
        CGFloat retweetedViewFW = contentViewMaxW;
        CGFloat retweetedViewFH = 0;
        
        //被转发用户的昵称
        CGFloat retweetedNameFX = LXStatusBorder;
        CGFloat retweetedNameFY = LXStatusBorder;
        CGSize retweetedNameLabelSize = [[NSString stringWithFormat:@"@%@",status.retweeted_status.user.screen_name] sizeWithAttributes:@{NSFontAttributeName:LXStatusNameFont}];
        CGFloat retweetedNameFW = retweetedNameLabelSize.width;
        CGFloat retweetedNameFH = retweetedNameLabelSize.height;
        self.retweetedNameF = CGRectMake(retweetedNameFX, retweetedNameFY, retweetedNameFW, retweetedNameFH);
        
        //用户的会员等级
        if(status.retweeted_status.user.mbtype){
            CGFloat retweetedVipViewFX = CGRectGetMaxX(self.retweetedNameF) + LXStatusBorder;
            CGFloat retweetedVipViewFY = LXStatusBorder;
            CGFloat retweetedVipViewFW = 14;
            CGFloat retweetedVipViewFH = retweetedNameFH;
            self.retweetedVipViewF = CGRectMake(retweetedVipViewFX, retweetedVipViewFY, retweetedVipViewFW, retweetedVipViewFH);
        }
 
        //被转发微博的正文
        CGFloat retweetedTextFX = LXStatusBorder;
        CGFloat retweetedTextFY = CGRectGetMaxY(self.retweetedNameF) + LXStatusBorder;
        CGFloat retweetedContentViewMaxW = retweetedViewFW - 2*LXStatusBorder;
        CGSize retweetedTextFSize = [status.retweeted_status.text boundingRectWithSize:CGSizeMake(retweetedContentViewMaxW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:LXStatusTextFont} context:nil].size;

        CGFloat retweetedTextFW = retweetedTextFSize.width;
        CGFloat retweetedTextFH = retweetedTextFSize.height;
        self.retweetedTextF = CGRectMake(retweetedTextFX, retweetedTextFY, retweetedTextFW, retweetedTextFH);
        
        //被转发微博的配图
        if(status.retweeted_status.pic_urls.count){
            CGFloat retweetedThumbnail_picFX = retweetedTextFX;
            CGFloat retweetedThumbnail_picFY = CGRectGetMaxY(self.retweetedTextF) + LXStatusBorder;
            CGFloat retweetedThumbnail_picFW = retweetedContentViewMaxW - 2 * LXStatusBorder;
            
            int maxCloumns = (status.pic_urls.count == 4) ? 2 : 3;
            int row = (status.retweeted_status.pic_urls.count / (double)maxCloumns) + 0.99;
            
            CGFloat retweetedThumbnail_picFH = LXStatusPhotoRowHeight * row;
            
            self.retweetedPic_urlsF = CGRectMake(retweetedThumbnail_picFX, retweetedThumbnail_picFY, retweetedThumbnail_picFW, retweetedThumbnail_picFH);
            
            retweetedViewFH = CGRectGetMaxY(self.retweetedPic_urlsF);
        }else{//无配图
            retweetedViewFH = CGRectGetMaxY(self.retweetedTextF);
        }
        self.retweetedViewF = CGRectMake(retweetedViewFX, retweetedViewFY, retweetedViewFW, retweetedViewFH + LXStatusBorder);
        
        topViewFH = CGRectGetMaxY(self.retweetedViewF);
    }else{ //无转发的微博
        //有配图
        if(status.pic_urls.count){
            topViewFH = CGRectGetMaxY(self.pic_urlsF);
        }else{
            topViewFH = CGRectGetMaxY(self.textF);
        }
    }
    
    self.topViewF = CGRectMake(topViewFX, topViewFY, topViewFW, topViewFH);
    
    //工具条
    CGFloat toolBarFX = topViewFX;
    CGFloat toolBarFY = CGRectGetMaxY(self.topViewF);
    CGFloat toolBarFW = topViewFW;
    CGFloat toolBarFH = 35;
    self.toolBarF = CGRectMake(toolBarFX, toolBarFY, toolBarFW, toolBarFH);
    
    //cell的高度
    self.cellHeight = CGRectGetMaxY(self.toolBarF) + LXStatusTableBorder * 2;
}

@end
